package com.csdj.web;

import com.alibaba.fastjson.JSON;
import com.csdj.service.EmployeeService;
import com.csdj.service.impl.EmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/reg")
public class registerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");

        String email = req.getParameter("email");

        EmployeeService employeeService = new EmployeeServiceImpl();
        PrintWriter out = resp.getWriter();
        Map<String, Object> map = new HashMap<>();

        if (employeeService.getEmail(email) > 0) {
            map.put("status", "error");
            map.put("msg", "该邮箱已注册！");
        } else {
            map.put("status", "success");
            map.put("msg", "可以使用！");
        }

        String json = JSON.toJSONString(map);
        out.write(json);
    }
}
